package com.ray.controller;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.ray.pojo.MyUser;
import com.ray.pojo.attend.DailySalaryDetailPojo;
import com.ray.service.AttendanceService;
import com.ray.service.DingTalkService;
import com.ray.service.DisplayViewService;
import com.ray.util.MathUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;

@RestController
@RequestMapping("/attendance")
public class AttendanceController {
	@Autowired
	AttendanceService attendanceService;
	@Autowired
	DisplayViewService displayViewService;
	@Autowired
	DingTalkService dingTalkService;
	
	/**
	 * 车间考勤看板
	 * @param request
	 * @param token
	 * @throws ParseException
	 * @author 王荣
	 */
	@RequestMapping("/getWorkshopAttendanceBoard")
	@SuppressWarnings("unchecked")
	public Map<String, Object> getWorkshopAttendanceBoard(HttpServletRequest request,String token) throws ParseException{
		MyUser myUser = null;
		try {
			myUser = displayViewService.checkLogin(request,token);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map=RequestUtil.getRequestParamByGeneric(request);
		if(1==myUser.getIsManager()) {
			
		}else if(JSONObject.toJSON(myUser.getPermissionList()).toString().indexOf("superManager")>-1) {
			
		}else if("1".equals(myUser.getDuty())) {
			Assert.isTrue(null!=myUser.getDeptId() && !myUser.getDeptId().equals(""),myUser.getUserName()+"经理还没有设置部门，请至职员表设置部门");
			map.put("deptId", myUser.getDeptId());
			List<String> deptIdList = new ArrayList<String>();
			deptIdList.add(myUser.getDeptId());
			map.put("deptIdList", deptIdList);
			map.put("workshopIdList", new ArrayList<>());
			map.put("userIdList", new ArrayList<>());
		}else if("2".equals(myUser.getDuty())) {
			Assert.isTrue(null!=myUser.getWorkShopId() && !myUser.getWorkShopId().equals(""),myUser.getUserName()+"主管还没有设置车间，请至职员表设置车间");
			map.put("workShopId", myUser.getWorkShopId());
			if("0".equals(myUser.getWorkShopId())) {
				List<String> deptIdList = new ArrayList<String>();
				deptIdList.add(myUser.getDeptId());
				map.put("deptIdList", deptIdList);
				map.put("workshopIdList", new ArrayList<>());
				map.put("userIdList", new ArrayList<>());
			}else {
				List<String> workshopIdList = new ArrayList<String>();
				List<String> deptIdList = new ArrayList<String>();
				deptIdList.add(myUser.getDeptId());
				map.put("deptIdList", deptIdList);
				workshopIdList.add(myUser.getWorkShopId());
				map.put("workshopIdList", workshopIdList);
				map.put("userIdList", new ArrayList<>());
			}
		}else {
			Assert.notNull(null,"该页面只有注册人、经理、主管能访问，请至职员表设置职位");
		}
		return ResponseUtil.ResponseResult(attendanceService.getWorkshopAttendanceBoard(map));
	}
	
	/** 查询车间当天考勤情况看板 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getCurrentDayAttendInfoBoard")
	public Map<String, Object> getCurrentDayAttendInfoBoard(HttpServletRequest request,String token) throws ParseException{
		MyUser myUser = null;
		try {
			myUser = displayViewService.checkLogin(request,token);
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String,Object> map=RequestUtil.getRequestParamByGeneric(request);
		if(1==myUser.getIsManager()) {
			
		}else if(JSONObject.toJSON(myUser.getPermissionList()).toString().indexOf("superManager")>-1) {
			
		}else if("1".equals(myUser.getDuty())) {
			Assert.isTrue(null!=myUser.getDeptId() && !myUser.getDeptId().equals(""),myUser.getUserName()+"经理还没有设置部门，请至职员表设置部门");
			map.put("deptId", myUser.getDeptId());
			List<String> deptIdList = new ArrayList<String>();
			deptIdList.add(myUser.getDeptId());
			map.put("deptIdList", deptIdList);
			map.put("workshopIdList", new ArrayList<>());
			map.put("userIdList", new ArrayList<>());
		}else if("2".equals(myUser.getDuty())) {
			Assert.isTrue(null!=myUser.getWorkShopId() && !myUser.getWorkShopId().equals(""),myUser.getUserName()+"主管还没有设置车间，请至职员表设置车间");
			map.put("workShopId", myUser.getWorkShopId());
			if("0".equals(myUser.getWorkShopId())) {
				List<String> deptIdList = new ArrayList<String>();
				deptIdList.add(myUser.getDeptId());
				map.put("deptIdList", deptIdList);
				map.put("workshopIdList", new ArrayList<>());
				map.put("userIdList", new ArrayList<>());
			}else {
				List<String> workshopIdList = new ArrayList<String>();
				List<String> deptIdList = new ArrayList<String>();
				deptIdList.add(myUser.getDeptId());
				map.put("deptIdList", deptIdList);
				workshopIdList.add(myUser.getWorkShopId());
				map.put("workshopIdList", workshopIdList);
				map.put("userIdList", new ArrayList<>());
			}
		}else {
			Assert.notNull(null,"该页面只有注册人、经理、主管能访问，请至职员表设置职位");
		}
		return ResponseUtil.ResponseResult(attendanceService.getCurrentDayAttendInfoBoard(map));
	}

	/**
	 * 获取某人某月的每日工资详情
	 * @param request
	 * @author 王荣
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getDailySalaryDetail")
	public Map<String,Object> getDailySalaryDetail(HttpServletRequest request){
		Map<String, Object> requestMap = RequestUtil.getRequestParamByGeneric(request);
		List<DailySalaryDetailPojo> dailySalaryDetailPojos = attendanceService.getDailySalaryDetail(requestMap);
		//没有每日工资明细记录时，会出现一条只有姓名的记录，移除
		if(dailySalaryDetailPojos.get(0).getWorkdate()==null) dailySalaryDetailPojos.remove(0);
		//日薪与时薪 显示4位小数
		for(DailySalaryDetailPojo dailySalaryDetailPojo : dailySalaryDetailPojos) {
			dailySalaryDetailPojo.setDateSalary(MathUtil.doubleFormat(dailySalaryDetailPojo.getDateSalary(), 4));
			dailySalaryDetailPojo.setHourlySalary(MathUtil.doubleFormat(dailySalaryDetailPojo.getHourlySalary(), 4));
		}
		return ResponseUtil.ResponseResult(dailySalaryDetailPojos);
	}
	
	/**
	 * 获取部门与车间，用于手机查看员工出勤统计的搜索
	 * @param request
	 * @author 王荣
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getDeptAndWorkshop")
	public Map<String,Object> getDeptAndWorkshop(HttpServletRequest request) {
		Map<String, Object> requestMap = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(attendanceService.getDeptAndWorkshop(requestMap));
	}

	/**
	 * 通过部门车间获取月统计信息 
	 * @param request
	 * @throws ParseException
	 * @author 王荣
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getMonthSummaryByDeptAndWorkshop")
	public Map<String,Object> getMonthSummaryByDeptAndWorkshop(HttpServletRequest request) throws ParseException {
		Map<String, Object> requestMap = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(attendanceService.getMonthSummaryByDeptAndWorkshop(requestMap));
	}
	
	
	/**
	 * 通过某一天应工作时长 
	 * @param request
	 * @throws ParseException
	 * @author 王荣
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/getWorkHourForEverDay")
	public Map<String,Object> getWorkHourForEverDay(HttpServletRequest request) throws ParseException {
		Map<String, Object> requestMap = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(attendanceService.getWorkHourForEverDay(requestMap));
	}
	
}
